package com.example.usercenter.sys.web;

import com.alibaba.fastjson.JSONObject;
import com.example.usercenter.common.base.ResponseData;
import com.example.usercenter.sys.entity.User;
import org.springframework.security.access.annotation.Secured;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.beans.factory.annotation.Autowired;

import com.example.usercenter.common.base.BaseRestController;
import com.example.usercenter.common.base.BaseService;
import com.example.usercenter.sys.service.UserGroupService;
import com.example.usercenter.sys.entity.UserGroup;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

@RestController
@RequestMapping(value="/userGroup")
public class UserGroupController extends BaseRestController<UserGroup, Long> {
    @Autowired
    private UserGroupService userGroupService;

    protected BaseService<UserGroup, Long> getService(){
            return userGroupService;
    }

    @Override
    @PreAuthorize("hasAuthority('USER_GROUP')")
    public ResponseData get(@PathVariable("id") Long id) {
        return super.get(id);
    }

    @Override
    @PreAuthorize("hasAuthority('USER_GROUP')")
    public ResponseData save(@RequestBody UserGroup entity) {
        return super.save(entity);
    }

    @Override
    @PreAuthorize("hasAuthority('USER_GROUP')")
    public ResponseData update(@RequestBody UserGroup entity) {
        return super.update(entity);
    }

    @Override
    @PreAuthorize("hasAuthority('USER_GROUP')")
    public ResponseData delete(@PathVariable("id") Long id) {
        return super.delete(id);
    }

    @Override
    @PreAuthorize("hasAuthority('USER_GROUP')")
    public ResponseData page(@RequestBody JSONObject params) {
        return super.page(params);
    }

    @Override
    @Secured({"USER_GROUP"})
    public ResponseData query(@RequestBody JSONObject params) {
        return super.query(params);
    }

    @RequestMapping(value = "/queryByUser")
    @Secured({"USER_GROUP", "USER", "ROLE", "AUTHORITY"})
    public ResponseData queryByUser(@RequestBody JSONObject params) {
        Authentication auth = SecurityContextHolder.getContext().getAuthentication();
        User user = (User) auth.getPrincipal();
        UserGroup group = user.getGroup();
        List<UserGroup> list = new ArrayList<UserGroup>();
        if(group.isChildAuth()){
            params.put("path", group.getId());
            params.put("sort", "path");
            list.addAll(userGroupService.query(params));
        }
        if(group.isPeerAuth()){
            if(params.get("autoAuth")==null || group.isAutoAuth()==params.getBoolean("autoAuth").booleanValue()){
                list.add(0, group);
            }
        }
        return ResponseData.ok("查询用户组成功", list);
    }

    @PreAuthorize("hasAuthority('USER_GROUP')")
    @Override
    public ResponseData exists(@RequestBody JSONObject params) {
        return super.exists(params);
    }
}